<?php

namespace App\Admin\Controllers;

use App\Extensions\OrderGender;
use App\Models\Menu;
use App\Models\News;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class NewsController extends BaseController
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            if ($menuId = request('menuId')) {
                $menu = Menu::find($menuId);
                session(['menu' => $menu]);
            } else {
                session(['menu' => null]);
                $menu = null;
            }
            $content->header('文章列表');
            $content->description('文章列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑文章');
            $content->description('编辑文章');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增文章');
            $content->description('新增文章');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(News::class, function (Grid $grid) {
            if ($menu = session('menu')) {
                $grid->model()->where('menu_id', $menu->id);
                $grid->disableExport();
                $grid->disableFilter();
            }
            $grid->model()->orderBy('sort','desc');
            $grid->tools(function ($tools) {
                //排序
                $tools->append(new OrderGender(News::class));
            });
            $grid->column('id');
            $grid->column('title', '标题');
            $grid->column('isShow', '显示')->switch(config('admin.status'));
            $grid->column('created_at', '创建时间');
            if ($menu && strstr($menu->newsEditShow, 'newsExtra')) {
                $grid->actions(function ($actions) {
                    $urlDetail = url('admin/newsExtra');
                    $actions->prepend("<a href='$urlDetail?newsId={$actions->getKey()}'><i class='fa fa-book'></i></a>");
                });
            }
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(News::class, function (Form $form) {

            $menu = session('menu');
            $form->hidden('sort')->default($form->model()->max('id') + 1);
            $show = $menu->newsEditShow;
            if (!$show || strstr($show, 'title'))
                $form->text('title', '标题');
            if (!$show || strstr($show, 'sub_title'))
                $form->text('sub_title', '副标题');
            $form->switch('isShow', '显示')->states(config('admin.status'));
            if (!$show || strstr($show, 'pic'))
                $form->image('pic', '封面')->uniqueName();
            if (!$show || strstr($show, 'pic_detail'))
                $form->image('pic_detail', '详情图')->uniqueName();
            if ($menu = session('menu')) {
                $form->hidden('menu_id')->default($menu->id);
                $form->saved(function (Form $form) use ($menu) {
                    return redirect("/admin/news?menuId={$menu->id}");
                });
            }
            if (!$show || strstr($show, 'content'))
                $form->editor('content', '内容');
            if (!$show || strstr($show, 'gallery'))
                $form->hasMany('gallery', '相册', function (Form\NestedForm $form) {
                    $form->image('pic', '原图')->uniqueName();
                    $form->image('thumPic', '缩略图')->uniqueName();
                });
        });
    }
}
